//
//  CircularProgressView.m
//  CircularProgressView
//


#import "CircularProgressView.h"

@interface CircularProgressView ()<AVAudioPlayerDelegate>



@property (assign, nonatomic) CGFloat angle;
@end

@implementation CircularProgressView

- (id)initWithFrame:(CGRect)frame
          backColor:(UIColor *)backColor
      progressColor:(UIColor *)progressColor
          lineWidth:(CGFloat)lineWidth
         {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _backColor = backColor;
        _progressColor = progressColor;
        _lineWidth = lineWidth;
        
       
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    //draw background circle
    
    UIBezierPath *backCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2)radius:(CGRectGetWidth(self.bounds) - self.lineWidth) / 2 startAngle:(CGFloat) - M_PI_2
                                                            endAngle:(CGFloat)(1.5 * M_PI) clockwise:YES];
    [self.backColor setStroke];
    backCircle.lineWidth = self.lineWidth;
    [backCircle stroke];

    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"pushprogress" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
//        NSString *duar = [note.userInfo objectForKey:@"duration"];
//        
//        NSString *curr = [note.userInfo objectForKey:@"current"];
        
        NSString *progress = [note.userInfo objectForKey:@"progress"];
        
        
        float floatString = [progress floatValue];
        
        self.progress = floatString;

        
        
    }];
    
    UIBezierPath *progressCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2) radius:(CGRectGetWidth(self.bounds) - self.lineWidth) / 2 startAngle:(CGFloat) - M_PI_2 endAngle:(CGFloat)(- M_PI_2 + M_PI * 2 * self.progress)
                                                               clockwise:YES];
    
    
    [self.progressColor setStroke];
    progressCircle.lineWidth = self.lineWidth;
    
    
    
    [progressCircle stroke];
    
    [self  setNeedsDisplay];
    
}



#pragma mark AVAudioPlayerDelegate metho


@end
